{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于矩阵分解(SVD)的音乐推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "import sys\n",
    "reload(sys)\n",
    "sys.setdefaultencoding(\"utf-8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "#print \"aaaa\"\n",
    "#import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据\n",
    "用户(10万)、歌曲（3万）、播放次数、歌曲元数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "triplet_dataset_sub_song_merged=pd.read_csv('triplet_dataset_sub_song_merged.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBONKR12A58A7A7E0</td>\n",
       "      <td>26</td>\n",
       "      <td>You're The One</td>\n",
       "      <td>If There Was A Way</td>\n",
       "      <td>Dwight Yoakam</td>\n",
       "      <td>1990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBZZDU12A6310D8A3</td>\n",
       "      <td>7</td>\n",
       "      <td>Don't Dream It's Over</td>\n",
       "      <td>Recurring Dream_ Best Of Crowded House (Domest...</td>\n",
       "      <td>Crowded House</td>\n",
       "      <td>1986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOCAHRT12A8C13A1A4</td>\n",
       "      <td>5</td>\n",
       "      <td>S.O.S.</td>\n",
       "      <td>SOS</td>\n",
       "      <td>Jonas Brothers</td>\n",
       "      <td>2007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODASIJ12A6D4F5D89</td>\n",
       "      <td>1</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>Michael Cretu</td>\n",
       "      <td>1985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODEAWL12AB0187032</td>\n",
       "      <td>8</td>\n",
       "      <td>American Idiot [feat. Green Day &amp; The Cast Of ...</td>\n",
       "      <td>The Original Broadway Cast Recording 'American...</td>\n",
       "      <td>Green Day</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "5  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBONKR12A58A7A7E0            26   \n",
       "6  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBZZDU12A6310D8A3             7   \n",
       "7  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOCAHRT12A8C13A1A4             5   \n",
       "8  d6589314c0a9bcbca4fee0c93b14bc402363afea  SODASIJ12A6D4F5D89             1   \n",
       "9  d6589314c0a9bcbca4fee0c93b14bc402363afea  SODEAWL12AB0187032             8   \n",
       "\n",
       "                                               title  \\\n",
       "0                                   You And Me Jesus   \n",
       "1                      Harder Better Faster Stronger   \n",
       "2                                           Uprising   \n",
       "3                             Breakfast At Tiffany's   \n",
       "4                              Lucky (Album Version)   \n",
       "5                                     You're The One   \n",
       "6                              Don't Dream It's Over   \n",
       "7                                             S.O.S.   \n",
       "8                                  The Invisible Man   \n",
       "9  American Idiot [feat. Green Day & The Cast Of ...   \n",
       "\n",
       "                                             release  \\\n",
       "0                               Tribute To Jake Hess   \n",
       "1                                          Discovery   \n",
       "2                                           Uprising   \n",
       "3                                               Home   \n",
       "4              We Sing.  We Dance.  We Steal Things.   \n",
       "5                                 If There Was A Way   \n",
       "6  Recurring Dream_ Best Of Crowded House (Domest...   \n",
       "7                                                SOS   \n",
       "8                                  The Invisible Man   \n",
       "9  The Original Broadway Cast Recording 'American...   \n",
       "\n",
       "                   artist_name  year  \n",
       "0                    Jake Hess  2004  \n",
       "1                    Daft Punk  2007  \n",
       "2                         Muse     0  \n",
       "3          Deep Blue Something  1993  \n",
       "4  Jason Mraz & Colbie Caillat     0  \n",
       "5                Dwight Yoakam  1990  \n",
       "6                Crowded House  1986  \n",
       "7               Jonas Brothers  2007  \n",
       "8                Michael Cretu  1985  \n",
       "9                    Green Day     0  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10775785, 7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "#分割数据为训练集与测试集\n",
    "#train_data, test_data = train_test_split(triplet_dataset_sub_song_merged, test_size = 0.40, random_state=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先计算歌曲被当前用户播放量 / 用户播放总量 当做分值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "\n",
       "                           title                                release  \\\n",
       "0               You And Me Jesus                   Tribute To Jake Hess   \n",
       "1  Harder Better Faster Stronger                              Discovery   \n",
       "2                       Uprising                               Uprising   \n",
       "3         Breakfast At Tiffany's                                   Home   \n",
       "4          Lucky (Album Version)  We Sing.  We Dance.  We Steal Things.   \n",
       "\n",
       "                   artist_name  year  total_listen_count  \n",
       "0                    Jake Hess  2004                 329  \n",
       "1                    Daft Punk  2007                 329  \n",
       "2                         Muse     0                 329  \n",
       "3          Deep Blue Something  1993                 329  \n",
       "4  Jason Mraz & Colbie Caillat     0                 329  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计每个用户总的歌曲点击量\n",
    "triplet_dataset_sub_song_merged_sum_df = triplet_dataset_sub_song_merged[['user','listen_count']].groupby('user').sum().reset_index()\n",
    "triplet_dataset_sub_song_merged_sum_df.rename(columns={'listen_count':'total_listen_count'},inplace=True)\n",
    "triplet_dataset_sub_song_merged = pd.merge(triplet_dataset_sub_song_merged,triplet_dataset_sub_song_merged_sum_df)\n",
    "triplet_dataset_sub_song_merged.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "      <td>329</td>\n",
       "      <td>0.036474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "\n",
       "                           title                                release  \\\n",
       "0               You And Me Jesus                   Tribute To Jake Hess   \n",
       "1  Harder Better Faster Stronger                              Discovery   \n",
       "2                       Uprising                               Uprising   \n",
       "3         Breakfast At Tiffany's                                   Home   \n",
       "4          Lucky (Album Version)  We Sing.  We Dance.  We Steal Things.   \n",
       "\n",
       "                   artist_name  year  total_listen_count     score  \n",
       "0                    Jake Hess  2004                 329  0.036474  \n",
       "1                    Daft Punk  2007                 329  0.003040  \n",
       "2                         Muse     0                 329  0.003040  \n",
       "3          Deep Blue Something  1993                 329  0.003040  \n",
       "4  Jason Mraz & Colbie Caillat     0                 329  0.021277  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算用户对每首歌的评分\n",
    "triplet_dataset_sub_song_merged['score'] =triplet_dataset_sub_song_merged['listen_count']/triplet_dataset_sub_song_merged['total_listen_count']\n",
    "triplet_dataset_sub_song_merged.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>329</td>\n",
       "      <td>0.036474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       user                song  listen_count  \\\n",
       "0  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82            12   \n",
       "1  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D             1   \n",
       "2  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239             1   \n",
       "3  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50             1   \n",
       "4  d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36             7   \n",
       "\n",
       "   total_listen_count     score  \n",
       "0                 329  0.036474  \n",
       "1                 329  0.003040  \n",
       "2                 329  0.003040  \n",
       "3                 329  0.003040  \n",
       "4                 329  0.021277  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triplet_dataset_sub_song_merged[['user','song','listen_count','total_listen_count','score']].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#构造分解矩阵\n",
    "from scipy.sparse import coo_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math as mt\n",
    "from scipy.sparse.linalg import * #used for matrix multiplication\n",
    "from scipy.sparse.linalg import svds\n",
    "#from scipy.sparse.linalg import svd\n",
    "from scipy.sparse import csc_matrix\n",
    "from numpy import linalg as la\n",
    "#from numpy import *\n",
    "#from scipy.sparse.linalg import * #used for matrix multiplication"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix_decomposition = triplet_dataset_sub_song_merged\n",
    "#用户索引表\n",
    "user_codes = matrix_decomposition.user.drop_duplicates().reset_index()\n",
    "user_codes.rename(columns={'index':'user_index'}, inplace=True)\n",
    "#user_codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "song_codes = matrix_decomposition.song.drop_duplicates().reset_index()\n",
    "song_codes.rename(columns={'index':'song_index'}, inplace=True)\n",
    "#song_codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存分解矩阵的行号和列号\n",
    "song_codes['so_index_value'] = list(song_codes.index)\n",
    "user_codes['us_index_value'] = list(user_codes.index)\n",
    "#user_codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>score</th>\n",
       "      <th>song_index</th>\n",
       "      <th>so_index_value</th>\n",
       "      <th>user_index</th>\n",
       "      <th>us_index_value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOADQPP12A67020C82</td>\n",
       "      <td>12</td>\n",
       "      <td>You And Me Jesus</td>\n",
       "      <td>Tribute To Jake Hess</td>\n",
       "      <td>Jake Hess</td>\n",
       "      <td>2004</td>\n",
       "      <td>329</td>\n",
       "      <td>0.036474</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAFTRR12AF72A8D4D</td>\n",
       "      <td>1</td>\n",
       "      <td>Harder Better Faster Stronger</td>\n",
       "      <td>Discovery</td>\n",
       "      <td>Daft Punk</td>\n",
       "      <td>2007</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOANQFY12AB0183239</td>\n",
       "      <td>1</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Uprising</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOAYATB12A6701FD50</td>\n",
       "      <td>1</td>\n",
       "      <td>Breakfast At Tiffany's</td>\n",
       "      <td>Home</td>\n",
       "      <td>Deep Blue Something</td>\n",
       "      <td>1993</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBOAFP12A8C131F36</td>\n",
       "      <td>7</td>\n",
       "      <td>Lucky (Album Version)</td>\n",
       "      <td>We Sing.  We Dance.  We Steal Things.</td>\n",
       "      <td>Jason Mraz &amp; Colbie Caillat</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBONKR12A58A7A7E0</td>\n",
       "      <td>26</td>\n",
       "      <td>You're The One</td>\n",
       "      <td>If There Was A Way</td>\n",
       "      <td>Dwight Yoakam</td>\n",
       "      <td>1990</td>\n",
       "      <td>329</td>\n",
       "      <td>0.079027</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOBZZDU12A6310D8A3</td>\n",
       "      <td>7</td>\n",
       "      <td>Don't Dream It's Over</td>\n",
       "      <td>Recurring Dream_ Best Of Crowded House (Domest...</td>\n",
       "      <td>Crowded House</td>\n",
       "      <td>1986</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOCAHRT12A8C13A1A4</td>\n",
       "      <td>5</td>\n",
       "      <td>S.O.S.</td>\n",
       "      <td>SOS</td>\n",
       "      <td>Jonas Brothers</td>\n",
       "      <td>2007</td>\n",
       "      <td>329</td>\n",
       "      <td>0.015198</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODASIJ12A6D4F5D89</td>\n",
       "      <td>1</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>The Invisible Man</td>\n",
       "      <td>Michael Cretu</td>\n",
       "      <td>1985</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODEAWL12AB0187032</td>\n",
       "      <td>8</td>\n",
       "      <td>American Idiot [feat. Green Day &amp; The Cast Of ...</td>\n",
       "      <td>The Original Broadway Cast Recording 'American...</td>\n",
       "      <td>Green Day</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.024316</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODJWHY12A8C142CCE</td>\n",
       "      <td>8</td>\n",
       "      <td>Hey_ Soul Sister</td>\n",
       "      <td>Save Me_ San Francisco</td>\n",
       "      <td>Train</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.024316</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODLLYS12A8C13A96B</td>\n",
       "      <td>4</td>\n",
       "      <td>Breakeven</td>\n",
       "      <td>Now That's What I Call Music! 72</td>\n",
       "      <td>The Script</td>\n",
       "      <td>2008</td>\n",
       "      <td>329</td>\n",
       "      <td>0.012158</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODLSCE12A6D4FBCAC</td>\n",
       "      <td>1</td>\n",
       "      <td>Pernambuco E Brasil</td>\n",
       "      <td>Bis</td>\n",
       "      <td>Moraes Moreira</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SODUGQV12AF72A6803</td>\n",
       "      <td>1</td>\n",
       "      <td>(You Drive Me) Crazy (The Stop Remix!)</td>\n",
       "      <td>The Singles Collection</td>\n",
       "      <td>Britney Spears</td>\n",
       "      <td>1999</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>13</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOEQJBS12A8AE475A4</td>\n",
       "      <td>6</td>\n",
       "      <td>The Funeral (Album Version)</td>\n",
       "      <td>The Road Mix: Music From The Television Series...</td>\n",
       "      <td>Band Of Horses</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.018237</td>\n",
       "      <td>14</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOFRCGW12A81C21EA6</td>\n",
       "      <td>7</td>\n",
       "      <td>Hey There Delilah</td>\n",
       "      <td>Now That's What I Call Music! 68</td>\n",
       "      <td>Plain White T's</td>\n",
       "      <td>2005</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOGJQBU12A6D4FA8EC</td>\n",
       "      <td>7</td>\n",
       "      <td>3AM (LP Version)</td>\n",
       "      <td>Yourself Or Someone Like You</td>\n",
       "      <td>matchbox twenty</td>\n",
       "      <td>1996</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOGTQNI12AB0184A5C</td>\n",
       "      <td>6</td>\n",
       "      <td>Vanilla Twilight</td>\n",
       "      <td>Ocean Eyes</td>\n",
       "      <td>Owl City</td>\n",
       "      <td>2009</td>\n",
       "      <td>329</td>\n",
       "      <td>0.018237</td>\n",
       "      <td>17</td>\n",
       "      <td>17</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOHFGKG12A6701C429</td>\n",
       "      <td>7</td>\n",
       "      <td>Picture</td>\n",
       "      <td>The Very Best Of Sheryl Crow</td>\n",
       "      <td>Sheryl Crow</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>18</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOISNSU12AC468C0D8</td>\n",
       "      <td>6</td>\n",
       "      <td>If I Had You</td>\n",
       "      <td>For Your Entertainment (Deluxe Version)</td>\n",
       "      <td>Adam Lambert</td>\n",
       "      <td>2009</td>\n",
       "      <td>329</td>\n",
       "      <td>0.018237</td>\n",
       "      <td>19</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOIWKTZ12A8C13571B</td>\n",
       "      <td>5</td>\n",
       "      <td>Bump Bounce Boogie (1990 Digital Remaster)</td>\n",
       "      <td>Texas Gold/Comin' Right At Ya</td>\n",
       "      <td>Asleep At The Wheel</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.015198</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOJTLHS12A8C13F633</td>\n",
       "      <td>1</td>\n",
       "      <td>Ain't No Rest For The Wicked (Original Version)</td>\n",
       "      <td>Cage The Elephant</td>\n",
       "      <td>Cage The Elephant</td>\n",
       "      <td>2008</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOKUPAO12AB018D576</td>\n",
       "      <td>4</td>\n",
       "      <td>The Only Exception (Album Version)</td>\n",
       "      <td>The Only Exception</td>\n",
       "      <td>Paramore</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.012158</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOLECYL12A58A7BF88</td>\n",
       "      <td>2</td>\n",
       "      <td>Black and White</td>\n",
       "      <td>Outside Inside</td>\n",
       "      <td>The String Cheese Incident</td>\n",
       "      <td>2001</td>\n",
       "      <td>329</td>\n",
       "      <td>0.006079</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOLFXKT12AB017E3E0</td>\n",
       "      <td>7</td>\n",
       "      <td>Fireflies</td>\n",
       "      <td>Karaoke Monthly Vol. 2 (January 2010)</td>\n",
       "      <td>Charttraxx Karaoke</td>\n",
       "      <td>2009</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>24</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SOLUHHS12A58A81A6B</td>\n",
       "      <td>8</td>\n",
       "      <td>The Trouble With Love Is</td>\n",
       "      <td>Beautiful</td>\n",
       "      <td>Kelly Clarkson</td>\n",
       "      <td>2003</td>\n",
       "      <td>329</td>\n",
       "      <td>0.024316</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SONHLJN12A81C2169B</td>\n",
       "      <td>1</td>\n",
       "      <td>Orange TrÃ¤gt Nur Die MÃ¼llabfuhr (Go West)</td>\n",
       "      <td>10 Jahre Gute Unterhaltung!</td>\n",
       "      <td>Mickie Krause</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.003040</td>\n",
       "      <td>26</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SONHWUN12AC468C014</td>\n",
       "      <td>9</td>\n",
       "      <td>Rianna</td>\n",
       "      <td>The Update Collection Vol. 2</td>\n",
       "      <td>Fisher</td>\n",
       "      <td>0</td>\n",
       "      <td>329</td>\n",
       "      <td>0.027356</td>\n",
       "      <td>27</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SONOYIB12A81C1F88C</td>\n",
       "      <td>7</td>\n",
       "      <td>I Kissed A Girl</td>\n",
       "      <td>One Of The Boys (iTunes Exclusive)</td>\n",
       "      <td>Katy Perry</td>\n",
       "      <td>2008</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>d6589314c0a9bcbca4fee0c93b14bc402363afea</td>\n",
       "      <td>SONYKOW12AB01849C9</td>\n",
       "      <td>7</td>\n",
       "      <td>Secrets</td>\n",
       "      <td>Waking Up</td>\n",
       "      <td>OneRepublic</td>\n",
       "      <td>2009</td>\n",
       "      <td>329</td>\n",
       "      <td>0.021277</td>\n",
       "      <td>29</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775755</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXAQJS12AB018CBF7</td>\n",
       "      <td>3</td>\n",
       "      <td>Resistance</td>\n",
       "      <td>The Resistance</td>\n",
       "      <td>Muse</td>\n",
       "      <td>0</td>\n",
       "      <td>764</td>\n",
       "      <td>0.003927</td>\n",
       "      <td>824</td>\n",
       "      <td>790</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775756</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXFPND12AB017C9D1</td>\n",
       "      <td>2</td>\n",
       "      <td>I Gotta Feeling</td>\n",
       "      <td>Todo Ãxitos 2009</td>\n",
       "      <td>Black Eyed Peas</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>826</td>\n",
       "      <td>792</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775757</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXLOQG12AF72A2D55</td>\n",
       "      <td>4</td>\n",
       "      <td>Unite (2009 Digital Remaster)</td>\n",
       "      <td>Hello Nasty (Remastered Edition)</td>\n",
       "      <td>Beastie Boys</td>\n",
       "      <td>0</td>\n",
       "      <td>764</td>\n",
       "      <td>0.005236</td>\n",
       "      <td>1496</td>\n",
       "      <td>1381</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775758</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXNZOW12AB017F756</td>\n",
       "      <td>3</td>\n",
       "      <td>Half Of My Heart</td>\n",
       "      <td>Battle Studies</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "      <td>764</td>\n",
       "      <td>0.003927</td>\n",
       "      <td>53</td>\n",
       "      <td>53</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775759</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXPFLM12A8AE48C50</td>\n",
       "      <td>3</td>\n",
       "      <td>Electric Feel</td>\n",
       "      <td>The Anthems</td>\n",
       "      <td>MGMT</td>\n",
       "      <td>2007</td>\n",
       "      <td>764</td>\n",
       "      <td>0.003927</td>\n",
       "      <td>828</td>\n",
       "      <td>793</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775760</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXQYSC12A6310E908</td>\n",
       "      <td>2</td>\n",
       "      <td>Bitter Sweet Symphony</td>\n",
       "      <td>Bitter Sweet Symphony</td>\n",
       "      <td>The Verve</td>\n",
       "      <td>1997</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>830</td>\n",
       "      <td>795</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775761</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXVVSM12A8C142224</td>\n",
       "      <td>2</td>\n",
       "      <td>Everlong</td>\n",
       "      <td>The Colour And The Shape (Special Edition)</td>\n",
       "      <td>Foo Fighters</td>\n",
       "      <td>1992</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>2400</td>\n",
       "      <td>2087</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775762</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXWYZP12AF72A42A6</td>\n",
       "      <td>3</td>\n",
       "      <td>Cosmic Love</td>\n",
       "      <td>Lungs</td>\n",
       "      <td>Florence + The Machine</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.003927</td>\n",
       "      <td>831</td>\n",
       "      <td>796</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775763</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOXZSEH12AC468CABB</td>\n",
       "      <td>1</td>\n",
       "      <td>Relegate the Judge</td>\n",
       "      <td>Live Gasometer 2009</td>\n",
       "      <td>Amity in fame</td>\n",
       "      <td>2008</td>\n",
       "      <td>764</td>\n",
       "      <td>0.001309</td>\n",
       "      <td>6239</td>\n",
       "      <td>4831</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775764</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYBWVL12AF72A3DD3</td>\n",
       "      <td>1</td>\n",
       "      <td>Immaculate Misconception</td>\n",
       "      <td>The Guessing Game</td>\n",
       "      <td>Cathedral</td>\n",
       "      <td>2010</td>\n",
       "      <td>764</td>\n",
       "      <td>0.001309</td>\n",
       "      <td>54</td>\n",
       "      <td>54</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775765</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYDTRQ12AF72A3D61</td>\n",
       "      <td>2</td>\n",
       "      <td>Be Somebody</td>\n",
       "      <td>Only By The Night</td>\n",
       "      <td>Kings Of Leon</td>\n",
       "      <td>2008</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>9545</td>\n",
       "      <td>6712</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775766</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYGHOJ12A6701FFBA</td>\n",
       "      <td>4</td>\n",
       "      <td>Hero</td>\n",
       "      <td>Greatest Hits</td>\n",
       "      <td>Enrique Iglesias</td>\n",
       "      <td>2001</td>\n",
       "      <td>764</td>\n",
       "      <td>0.005236</td>\n",
       "      <td>832</td>\n",
       "      <td>797</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775767</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYGHUM12AB018139C</td>\n",
       "      <td>4</td>\n",
       "      <td>Bad Company</td>\n",
       "      <td>War Is The Answer</td>\n",
       "      <td>Five Finger Death Punch</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.005236</td>\n",
       "      <td>833</td>\n",
       "      <td>798</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775768</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYGKNI12AB0187E6E</td>\n",
       "      <td>2</td>\n",
       "      <td>All I Do Is Win (feat. T-Pain_ Ludacris_ Snoop...</td>\n",
       "      <td>Victory</td>\n",
       "      <td>DJ Khaled</td>\n",
       "      <td>2010</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>834</td>\n",
       "      <td>799</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775769</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYJDRA12AB017F30D</td>\n",
       "      <td>2</td>\n",
       "      <td>First Dance</td>\n",
       "      <td>My Worlds</td>\n",
       "      <td>Justin Bieber / Usher</td>\n",
       "      <td>2010</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>6240</td>\n",
       "      <td>4832</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775770</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOYMIMI12AB0181E5C</td>\n",
       "      <td>4</td>\n",
       "      <td>Heads Will Roll</td>\n",
       "      <td>It's Blitz!</td>\n",
       "      <td>Yeah Yeah Yeahs</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.005236</td>\n",
       "      <td>836</td>\n",
       "      <td>801</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775771</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZCDWG12A6D4F81E1</td>\n",
       "      <td>1</td>\n",
       "      <td>A Beggar On A Beach Of Gold</td>\n",
       "      <td>Beggar On A Beach Of Gold</td>\n",
       "      <td>Mike And The Mechanics</td>\n",
       "      <td>1995</td>\n",
       "      <td>764</td>\n",
       "      <td>0.001309</td>\n",
       "      <td>4745</td>\n",
       "      <td>3834</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775772</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZDGEW12A8C13E748</td>\n",
       "      <td>4</td>\n",
       "      <td>One</td>\n",
       "      <td>...And Justice For All</td>\n",
       "      <td>Metallica</td>\n",
       "      <td>1988</td>\n",
       "      <td>764</td>\n",
       "      <td>0.005236</td>\n",
       "      <td>838</td>\n",
       "      <td>803</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775773</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZEETS12AC9071BD3</td>\n",
       "      <td>1</td>\n",
       "      <td>Favorite Girl</td>\n",
       "      <td>My World</td>\n",
       "      <td>Justin Bieber</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.001309</td>\n",
       "      <td>839</td>\n",
       "      <td>804</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775774</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZKNSB12A8C140F11</td>\n",
       "      <td>1</td>\n",
       "      <td>Replay</td>\n",
       "      <td>Massive R&amp;B Spring 2010</td>\n",
       "      <td>Iyaz</td>\n",
       "      <td>2009</td>\n",
       "      <td>764</td>\n",
       "      <td>0.001309</td>\n",
       "      <td>8369</td>\n",
       "      <td>6184</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775775</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZVCRW12A67ADA0B7</td>\n",
       "      <td>2</td>\n",
       "      <td>When You Were Young</td>\n",
       "      <td>When You  Were Young</td>\n",
       "      <td>The Killers</td>\n",
       "      <td>2006</td>\n",
       "      <td>764</td>\n",
       "      <td>0.002618</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775776</th>\n",
       "      <td>8305c896f42308824da7d4386f4b9ee584281412</td>\n",
       "      <td>SOZVUCT12A8C1424BE</td>\n",
       "      <td>5</td>\n",
       "      <td>Lasso</td>\n",
       "      <td>Wolfgang Amadeus Phoenix</td>\n",
       "      <td>Phoenix</td>\n",
       "      <td>0</td>\n",
       "      <td>764</td>\n",
       "      <td>0.006545</td>\n",
       "      <td>8875</td>\n",
       "      <td>6398</td>\n",
       "      <td>10775522</td>\n",
       "      <td>99994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775777</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SODPCMA12A6D4FC1E8</td>\n",
       "      <td>1</td>\n",
       "      <td>Tonight</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2008</td>\n",
       "      <td>19</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>9723</td>\n",
       "      <td>6802</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775778</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOEYYLH12A8C1330FE</td>\n",
       "      <td>1</td>\n",
       "      <td>Melodies &amp; Desires</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2008</td>\n",
       "      <td>19</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>9739</td>\n",
       "      <td>6811</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775779</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOFOYKG12A8C133122</td>\n",
       "      <td>1</td>\n",
       "      <td>Time Flies</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2007</td>\n",
       "      <td>19</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>9742</td>\n",
       "      <td>6812</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775780</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOFVUHW12A8C133115</td>\n",
       "      <td>1</td>\n",
       "      <td>Hanging High</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2008</td>\n",
       "      <td>19</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>9747</td>\n",
       "      <td>6814</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775781</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOKTYPK12A8C13AD20</td>\n",
       "      <td>10</td>\n",
       "      <td>Miss Take (Album Version)</td>\n",
       "      <td>Hell Yeah</td>\n",
       "      <td>HorrorPops</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>0.526316</td>\n",
       "      <td>8119</td>\n",
       "      <td>6010</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775782</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOTJPUL12A8C133126</td>\n",
       "      <td>1</td>\n",
       "      <td>Window Blues</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2008</td>\n",
       "      <td>19</td>\n",
       "      <td>0.052632</td>\n",
       "      <td>1385</td>\n",
       "      <td>1292</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775783</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOUEAAS12A8C133106</td>\n",
       "      <td>2</td>\n",
       "      <td>Let It Fall</td>\n",
       "      <td>Youth Novels</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>2008</td>\n",
       "      <td>19</td>\n",
       "      <td>0.105263</td>\n",
       "      <td>1392</td>\n",
       "      <td>1298</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10775784</th>\n",
       "      <td>ca99d94daa9d5231643a08aac2f3bfb645e73b09</td>\n",
       "      <td>SOXEUDZ12A8C13145B</td>\n",
       "      <td>2</td>\n",
       "      <td>I'm Good_ I'm Gone</td>\n",
       "      <td>I'm Good_ I'm Gone</td>\n",
       "      <td>Lykke Li</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>0.105263</td>\n",
       "      <td>1415</td>\n",
       "      <td>1317</td>\n",
       "      <td>10775777</td>\n",
       "      <td>99995</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10775785 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              user                song  \\\n",
       "0         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOADQPP12A67020C82   \n",
       "1         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAFTRR12AF72A8D4D   \n",
       "2         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOANQFY12AB0183239   \n",
       "3         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOAYATB12A6701FD50   \n",
       "4         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBOAFP12A8C131F36   \n",
       "5         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBONKR12A58A7A7E0   \n",
       "6         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOBZZDU12A6310D8A3   \n",
       "7         d6589314c0a9bcbca4fee0c93b14bc402363afea  SOCAHRT12A8C13A1A4   \n",
       "8         d6589314c0a9bcbca4fee0c93b14bc402363afea  SODASIJ12A6D4F5D89   \n",
       "9         d6589314c0a9bcbca4fee0c93b14bc402363afea  SODEAWL12AB0187032   \n",
       "10        d6589314c0a9bcbca4fee0c93b14bc402363afea  SODJWHY12A8C142CCE   \n",
       "11        d6589314c0a9bcbca4fee0c93b14bc402363afea  SODLLYS12A8C13A96B   \n",
       "12        d6589314c0a9bcbca4fee0c93b14bc402363afea  SODLSCE12A6D4FBCAC   \n",
       "13        d6589314c0a9bcbca4fee0c93b14bc402363afea  SODUGQV12AF72A6803   \n",
       "14        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOEQJBS12A8AE475A4   \n",
       "15        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOFRCGW12A81C21EA6   \n",
       "16        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOGJQBU12A6D4FA8EC   \n",
       "17        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOGTQNI12AB0184A5C   \n",
       "18        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOHFGKG12A6701C429   \n",
       "19        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOISNSU12AC468C0D8   \n",
       "20        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOIWKTZ12A8C13571B   \n",
       "21        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOJTLHS12A8C13F633   \n",
       "22        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOKUPAO12AB018D576   \n",
       "23        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOLECYL12A58A7BF88   \n",
       "24        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOLFXKT12AB017E3E0   \n",
       "25        d6589314c0a9bcbca4fee0c93b14bc402363afea  SOLUHHS12A58A81A6B   \n",
       "26        d6589314c0a9bcbca4fee0c93b14bc402363afea  SONHLJN12A81C2169B   \n",
       "27        d6589314c0a9bcbca4fee0c93b14bc402363afea  SONHWUN12AC468C014   \n",
       "28        d6589314c0a9bcbca4fee0c93b14bc402363afea  SONOYIB12A81C1F88C   \n",
       "29        d6589314c0a9bcbca4fee0c93b14bc402363afea  SONYKOW12AB01849C9   \n",
       "...                                            ...                 ...   \n",
       "10775755  8305c896f42308824da7d4386f4b9ee584281412  SOXAQJS12AB018CBF7   \n",
       "10775756  8305c896f42308824da7d4386f4b9ee584281412  SOXFPND12AB017C9D1   \n",
       "10775757  8305c896f42308824da7d4386f4b9ee584281412  SOXLOQG12AF72A2D55   \n",
       "10775758  8305c896f42308824da7d4386f4b9ee584281412  SOXNZOW12AB017F756   \n",
       "10775759  8305c896f42308824da7d4386f4b9ee584281412  SOXPFLM12A8AE48C50   \n",
       "10775760  8305c896f42308824da7d4386f4b9ee584281412  SOXQYSC12A6310E908   \n",
       "10775761  8305c896f42308824da7d4386f4b9ee584281412  SOXVVSM12A8C142224   \n",
       "10775762  8305c896f42308824da7d4386f4b9ee584281412  SOXWYZP12AF72A42A6   \n",
       "10775763  8305c896f42308824da7d4386f4b9ee584281412  SOXZSEH12AC468CABB   \n",
       "10775764  8305c896f42308824da7d4386f4b9ee584281412  SOYBWVL12AF72A3DD3   \n",
       "10775765  8305c896f42308824da7d4386f4b9ee584281412  SOYDTRQ12AF72A3D61   \n",
       "10775766  8305c896f42308824da7d4386f4b9ee584281412  SOYGHOJ12A6701FFBA   \n",
       "10775767  8305c896f42308824da7d4386f4b9ee584281412  SOYGHUM12AB018139C   \n",
       "10775768  8305c896f42308824da7d4386f4b9ee584281412  SOYGKNI12AB0187E6E   \n",
       "10775769  8305c896f42308824da7d4386f4b9ee584281412  SOYJDRA12AB017F30D   \n",
       "10775770  8305c896f42308824da7d4386f4b9ee584281412  SOYMIMI12AB0181E5C   \n",
       "10775771  8305c896f42308824da7d4386f4b9ee584281412  SOZCDWG12A6D4F81E1   \n",
       "10775772  8305c896f42308824da7d4386f4b9ee584281412  SOZDGEW12A8C13E748   \n",
       "10775773  8305c896f42308824da7d4386f4b9ee584281412  SOZEETS12AC9071BD3   \n",
       "10775774  8305c896f42308824da7d4386f4b9ee584281412  SOZKNSB12A8C140F11   \n",
       "10775775  8305c896f42308824da7d4386f4b9ee584281412  SOZVCRW12A67ADA0B7   \n",
       "10775776  8305c896f42308824da7d4386f4b9ee584281412  SOZVUCT12A8C1424BE   \n",
       "10775777  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SODPCMA12A6D4FC1E8   \n",
       "10775778  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOEYYLH12A8C1330FE   \n",
       "10775779  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOFOYKG12A8C133122   \n",
       "10775780  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOFVUHW12A8C133115   \n",
       "10775781  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOKTYPK12A8C13AD20   \n",
       "10775782  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOTJPUL12A8C133126   \n",
       "10775783  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOUEAAS12A8C133106   \n",
       "10775784  ca99d94daa9d5231643a08aac2f3bfb645e73b09  SOXEUDZ12A8C13145B   \n",
       "\n",
       "          listen_count                                              title  \\\n",
       "0                   12                                   You And Me Jesus   \n",
       "1                    1                      Harder Better Faster Stronger   \n",
       "2                    1                                           Uprising   \n",
       "3                    1                             Breakfast At Tiffany's   \n",
       "4                    7                              Lucky (Album Version)   \n",
       "5                   26                                     You're The One   \n",
       "6                    7                              Don't Dream It's Over   \n",
       "7                    5                                             S.O.S.   \n",
       "8                    1                                  The Invisible Man   \n",
       "9                    8  American Idiot [feat. Green Day & The Cast Of ...   \n",
       "10                   8                                   Hey_ Soul Sister   \n",
       "11                   4                                          Breakeven   \n",
       "12                   1                                Pernambuco E Brasil   \n",
       "13                   1             (You Drive Me) Crazy (The Stop Remix!)   \n",
       "14                   6                        The Funeral (Album Version)   \n",
       "15                   7                                  Hey There Delilah   \n",
       "16                   7                                   3AM (LP Version)   \n",
       "17                   6                                   Vanilla Twilight   \n",
       "18                   7                                            Picture   \n",
       "19                   6                                       If I Had You   \n",
       "20                   5         Bump Bounce Boogie (1990 Digital Remaster)   \n",
       "21                   1    Ain't No Rest For The Wicked (Original Version)   \n",
       "22                   4                 The Only Exception (Album Version)   \n",
       "23                   2                                    Black and White   \n",
       "24                   7                                          Fireflies   \n",
       "25                   8                           The Trouble With Love Is   \n",
       "26                   1        Orange TrÃ¤gt Nur Die MÃ¼llabfuhr (Go West)   \n",
       "27                   9                                             Rianna   \n",
       "28                   7                                    I Kissed A Girl   \n",
       "29                   7                                            Secrets   \n",
       "...                ...                                                ...   \n",
       "10775755             3                                         Resistance   \n",
       "10775756             2                                    I Gotta Feeling   \n",
       "10775757             4                      Unite (2009 Digital Remaster)   \n",
       "10775758             3                                   Half Of My Heart   \n",
       "10775759             3                                      Electric Feel   \n",
       "10775760             2                              Bitter Sweet Symphony   \n",
       "10775761             2                                           Everlong   \n",
       "10775762             3                                        Cosmic Love   \n",
       "10775763             1                                 Relegate the Judge   \n",
       "10775764             1                           Immaculate Misconception   \n",
       "10775765             2                                        Be Somebody   \n",
       "10775766             4                                               Hero   \n",
       "10775767             4                                        Bad Company   \n",
       "10775768             2  All I Do Is Win (feat. T-Pain_ Ludacris_ Snoop...   \n",
       "10775769             2                                        First Dance   \n",
       "10775770             4                                    Heads Will Roll   \n",
       "10775771             1                        A Beggar On A Beach Of Gold   \n",
       "10775772             4                                                One   \n",
       "10775773             1                                      Favorite Girl   \n",
       "10775774             1                                             Replay   \n",
       "10775775             2                                When You Were Young   \n",
       "10775776             5                                              Lasso   \n",
       "10775777             1                                            Tonight   \n",
       "10775778             1                                 Melodies & Desires   \n",
       "10775779             1                                         Time Flies   \n",
       "10775780             1                                       Hanging High   \n",
       "10775781            10                          Miss Take (Album Version)   \n",
       "10775782             1                                       Window Blues   \n",
       "10775783             2                                        Let It Fall   \n",
       "10775784             2                                 I'm Good_ I'm Gone   \n",
       "\n",
       "                                                    release  \\\n",
       "0                                      Tribute To Jake Hess   \n",
       "1                                                 Discovery   \n",
       "2                                                  Uprising   \n",
       "3                                                      Home   \n",
       "4                     We Sing.  We Dance.  We Steal Things.   \n",
       "5                                        If There Was A Way   \n",
       "6         Recurring Dream_ Best Of Crowded House (Domest...   \n",
       "7                                                       SOS   \n",
       "8                                         The Invisible Man   \n",
       "9         The Original Broadway Cast Recording 'American...   \n",
       "10                                   Save Me_ San Francisco   \n",
       "11                         Now That's What I Call Music! 72   \n",
       "12                                                      Bis   \n",
       "13                                   The Singles Collection   \n",
       "14        The Road Mix: Music From The Television Series...   \n",
       "15                         Now That's What I Call Music! 68   \n",
       "16                             Yourself Or Someone Like You   \n",
       "17                                               Ocean Eyes   \n",
       "18                             The Very Best Of Sheryl Crow   \n",
       "19                  For Your Entertainment (Deluxe Version)   \n",
       "20                            Texas Gold/Comin' Right At Ya   \n",
       "21                                        Cage The Elephant   \n",
       "22                                       The Only Exception   \n",
       "23                                           Outside Inside   \n",
       "24                    Karaoke Monthly Vol. 2 (January 2010)   \n",
       "25                                                Beautiful   \n",
       "26                              10 Jahre Gute Unterhaltung!   \n",
       "27                             The Update Collection Vol. 2   \n",
       "28                       One Of The Boys (iTunes Exclusive)   \n",
       "29                                                Waking Up   \n",
       "...                                                     ...   \n",
       "10775755                                     The Resistance   \n",
       "10775756                                  Todo Ãxitos 2009   \n",
       "10775757                   Hello Nasty (Remastered Edition)   \n",
       "10775758                                     Battle Studies   \n",
       "10775759                                        The Anthems   \n",
       "10775760                              Bitter Sweet Symphony   \n",
       "10775761         The Colour And The Shape (Special Edition)   \n",
       "10775762                                              Lungs   \n",
       "10775763                                Live Gasometer 2009   \n",
       "10775764                                  The Guessing Game   \n",
       "10775765                                  Only By The Night   \n",
       "10775766                                      Greatest Hits   \n",
       "10775767                                  War Is The Answer   \n",
       "10775768                                            Victory   \n",
       "10775769                                          My Worlds   \n",
       "10775770                                        It's Blitz!   \n",
       "10775771                          Beggar On A Beach Of Gold   \n",
       "10775772                             ...And Justice For All   \n",
       "10775773                                           My World   \n",
       "10775774                            Massive R&B Spring 2010   \n",
       "10775775                               When You  Were Young   \n",
       "10775776                           Wolfgang Amadeus Phoenix   \n",
       "10775777                                       Youth Novels   \n",
       "10775778                                       Youth Novels   \n",
       "10775779                                       Youth Novels   \n",
       "10775780                                       Youth Novels   \n",
       "10775781                                          Hell Yeah   \n",
       "10775782                                       Youth Novels   \n",
       "10775783                                       Youth Novels   \n",
       "10775784                                 I'm Good_ I'm Gone   \n",
       "\n",
       "                          artist_name  year  total_listen_count     score  \\\n",
       "0                           Jake Hess  2004                 329  0.036474   \n",
       "1                           Daft Punk  2007                 329  0.003040   \n",
       "2                                Muse     0                 329  0.003040   \n",
       "3                 Deep Blue Something  1993                 329  0.003040   \n",
       "4         Jason Mraz & Colbie Caillat     0                 329  0.021277   \n",
       "5                       Dwight Yoakam  1990                 329  0.079027   \n",
       "6                       Crowded House  1986                 329  0.021277   \n",
       "7                      Jonas Brothers  2007                 329  0.015198   \n",
       "8                       Michael Cretu  1985                 329  0.003040   \n",
       "9                           Green Day     0                 329  0.024316   \n",
       "10                              Train     0                 329  0.024316   \n",
       "11                         The Script  2008                 329  0.012158   \n",
       "12                     Moraes Moreira     0                 329  0.003040   \n",
       "13                     Britney Spears  1999                 329  0.003040   \n",
       "14                     Band Of Horses     0                 329  0.018237   \n",
       "15                    Plain White T's  2005                 329  0.021277   \n",
       "16                    matchbox twenty  1996                 329  0.021277   \n",
       "17                           Owl City  2009                 329  0.018237   \n",
       "18                        Sheryl Crow     0                 329  0.021277   \n",
       "19                       Adam Lambert  2009                 329  0.018237   \n",
       "20                Asleep At The Wheel     0                 329  0.015198   \n",
       "21                  Cage The Elephant  2008                 329  0.003040   \n",
       "22                           Paramore     0                 329  0.012158   \n",
       "23         The String Cheese Incident  2001                 329  0.006079   \n",
       "24                 Charttraxx Karaoke  2009                 329  0.021277   \n",
       "25                     Kelly Clarkson  2003                 329  0.024316   \n",
       "26                      Mickie Krause     0                 329  0.003040   \n",
       "27                             Fisher     0                 329  0.027356   \n",
       "28                         Katy Perry  2008                 329  0.021277   \n",
       "29                        OneRepublic  2009                 329  0.021277   \n",
       "...                               ...   ...                 ...       ...   \n",
       "10775755                         Muse     0                 764  0.003927   \n",
       "10775756              Black Eyed Peas  2009                 764  0.002618   \n",
       "10775757                 Beastie Boys     0                 764  0.005236   \n",
       "10775758                   John Mayer     0                 764  0.003927   \n",
       "10775759                         MGMT  2007                 764  0.003927   \n",
       "10775760                    The Verve  1997                 764  0.002618   \n",
       "10775761                 Foo Fighters  1992                 764  0.002618   \n",
       "10775762       Florence + The Machine  2009                 764  0.003927   \n",
       "10775763                Amity in fame  2008                 764  0.001309   \n",
       "10775764                    Cathedral  2010                 764  0.001309   \n",
       "10775765                Kings Of Leon  2008                 764  0.002618   \n",
       "10775766             Enrique Iglesias  2001                 764  0.005236   \n",
       "10775767      Five Finger Death Punch  2009                 764  0.005236   \n",
       "10775768                    DJ Khaled  2010                 764  0.002618   \n",
       "10775769        Justin Bieber / Usher  2010                 764  0.002618   \n",
       "10775770              Yeah Yeah Yeahs  2009                 764  0.005236   \n",
       "10775771       Mike And The Mechanics  1995                 764  0.001309   \n",
       "10775772                    Metallica  1988                 764  0.005236   \n",
       "10775773                Justin Bieber  2009                 764  0.001309   \n",
       "10775774                         Iyaz  2009                 764  0.001309   \n",
       "10775775                  The Killers  2006                 764  0.002618   \n",
       "10775776                      Phoenix     0                 764  0.006545   \n",
       "10775777                     Lykke Li  2008                  19  0.052632   \n",
       "10775778                     Lykke Li  2008                  19  0.052632   \n",
       "10775779                     Lykke Li  2007                  19  0.052632   \n",
       "10775780                     Lykke Li  2008                  19  0.052632   \n",
       "10775781                   HorrorPops     0                  19  0.526316   \n",
       "10775782                     Lykke Li  2008                  19  0.052632   \n",
       "10775783                     Lykke Li  2008                  19  0.105263   \n",
       "10775784                     Lykke Li     0                  19  0.105263   \n",
       "\n",
       "          song_index  so_index_value  user_index  us_index_value  \n",
       "0                  0               0           0               0  \n",
       "1                  1               1           0               0  \n",
       "2                  2               2           0               0  \n",
       "3                  3               3           0               0  \n",
       "4                  4               4           0               0  \n",
       "5                  5               5           0               0  \n",
       "6                  6               6           0               0  \n",
       "7                  7               7           0               0  \n",
       "8                  8               8           0               0  \n",
       "9                  9               9           0               0  \n",
       "10                10              10           0               0  \n",
       "11                11              11           0               0  \n",
       "12                12              12           0               0  \n",
       "13                13              13           0               0  \n",
       "14                14              14           0               0  \n",
       "15                15              15           0               0  \n",
       "16                16              16           0               0  \n",
       "17                17              17           0               0  \n",
       "18                18              18           0               0  \n",
       "19                19              19           0               0  \n",
       "20                20              20           0               0  \n",
       "21                21              21           0               0  \n",
       "22                22              22           0               0  \n",
       "23                23              23           0               0  \n",
       "24                24              24           0               0  \n",
       "25                25              25           0               0  \n",
       "26                26              26           0               0  \n",
       "27                27              27           0               0  \n",
       "28                28              28           0               0  \n",
       "29                29              29           0               0  \n",
       "...              ...             ...         ...             ...  \n",
       "10775755         824             790    10775522           99994  \n",
       "10775756         826             792    10775522           99994  \n",
       "10775757        1496            1381    10775522           99994  \n",
       "10775758          53              53    10775522           99994  \n",
       "10775759         828             793    10775522           99994  \n",
       "10775760         830             795    10775522           99994  \n",
       "10775761        2400            2087    10775522           99994  \n",
       "10775762         831             796    10775522           99994  \n",
       "10775763        6239            4831    10775522           99994  \n",
       "10775764          54              54    10775522           99994  \n",
       "10775765        9545            6712    10775522           99994  \n",
       "10775766         832             797    10775522           99994  \n",
       "10775767         833             798    10775522           99994  \n",
       "10775768         834             799    10775522           99994  \n",
       "10775769        6240            4832    10775522           99994  \n",
       "10775770         836             801    10775522           99994  \n",
       "10775771        4745            3834    10775522           99994  \n",
       "10775772         838             803    10775522           99994  \n",
       "10775773         839             804    10775522           99994  \n",
       "10775774        8369            6184    10775522           99994  \n",
       "10775775          57              57    10775522           99994  \n",
       "10775776        8875            6398    10775522           99994  \n",
       "10775777        9723            6802    10775777           99995  \n",
       "10775778        9739            6811    10775777           99995  \n",
       "10775779        9742            6812    10775777           99995  \n",
       "10775780        9747            6814    10775777           99995  \n",
       "10775781        8119            6010    10775777           99995  \n",
       "10775782        1385            1292    10775777           99995  \n",
       "10775783        1392            1298    10775777           99995  \n",
       "10775784        1415            1317    10775777           99995  \n",
       "\n",
       "[10775785 rows x 13 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_decomposition = pd.merge(matrix_decomposition,song_codes,how='left')\n",
    "matrix_decomposition = pd.merge(matrix_decomposition,user_codes,how='left')\n",
    "matrix_decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "mat_candidate = matrix_decomposition[['us_index_value','so_index_value','score']]\n",
    "data_array = mat_candidate.score.values\n",
    "row_array = mat_candidate.us_index_value.values\n",
    "col_array = mat_candidate.so_index_value.values\n",
    "\n",
    "data_sparse = coo_matrix((data_array, (row_array, col_array)),dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<99996x30000 sparse matrix of type '<type 'numpy.float64'>'\n",
       "\twith 10775785 stored elements in COOrdinate format>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_sparse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_index</th>\n",
       "      <th>user</th>\n",
       "      <th>us_index_value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>48515</th>\n",
       "      <td>5232517</td>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>48515</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_index                                      user  us_index_value\n",
       "48515     5232517  050834fcdba55aa3e6ada1d4f2949002ee817254           48515"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_codes[user_codes.user =='050834fcdba55aa3e6ada1d4f2949002ee817254']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7    b61afb42335287239bd40e1dea50d849cbf8a9a9\n",
       "Name: user, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_codes[user_codes.us_index_value ==7].user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_svd(urm, K,percentage):\n",
    "    U, s, Vt = svds(urm, K)\n",
    "    #U, s, Vt = la.svd(urm)\n",
    "    #sigma2=s**2\n",
    "    sigma2=s\n",
    "    sumsgm2=sum(sigma2)\n",
    "    sumsgm3=0 #sumsgm3是前k个奇异值和\n",
    "    k=0\n",
    "    for i in s:\n",
    "        #sumsgm3+=i**2\n",
    "        sumsgm3+=i\n",
    "        k+=1\n",
    "        if sumsgm3>=sumsgm2*percentage:\n",
    "            break\n",
    "    S=np.mat(np.eye(k)*s[:k])  #构建对角矩阵\n",
    "    #print \"%d\" %k\n",
    "    '''\n",
    "    dim = (len(s), len(s))\n",
    "    S = np.zeros(dim, dtype=np.float32)\n",
    "    for i in range(0, len(s)):\n",
    "        S[i,i] = mt.sqrt(s[i])\n",
    "    '''\n",
    "    U=U[:,:k]\n",
    "    Vt=Vt[:k,:]\n",
    "    U = csc_matrix(U, dtype=np.float32)\n",
    "    S = csc_matrix(S, dtype=np.float32)\n",
    "    Vt = csc_matrix(Vt, dtype=np.float32)\n",
    "    \n",
    "    return U, S, Vt,k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#由于数据量太多，这里选择K=100做参数\n",
    "K=100\n",
    "urm = data_sparse\n",
    "MAX_PID = urm.shape[1]\n",
    "MAX_UID = urm.shape[0]\n",
    "percentage=0.9\n",
    "U, S, Vt ,K= compute_svd(urm, K,percentage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "97"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K#最后选取的特征维数为97维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_estimated_matrix(urm, U, S, Vt, uTest, K, test):\n",
    "    rightTerm = S*Vt \n",
    "    max_recommendation = 1000\n",
    "    estimatedRatings = np.zeros(shape=(MAX_UID, MAX_PID), dtype=np.float16)\n",
    "    recomendRatings = np.zeros(shape=(MAX_UID,max_recommendation ), dtype=np.float16)\n",
    "    for userTest in uTest:\n",
    "        prod = U[userTest, :]*rightTerm\n",
    "        estimatedRatings[userTest, :] = prod.todense()\n",
    "        #urm_array=urm.toarray()\n",
    "        #for i in range(30000):\n",
    "            #if(urm[userTest][i]!=0):\n",
    "                #estimatedRatings[userTest,i]=0\n",
    "        recomendRatings[userTest, :] = (-estimatedRatings[userTest, :]).argsort()[:max_recommendation]\n",
    "    return recomendRatings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "uTest = [48515]\n",
    "uTest_recommended_items = compute_estimated_matrix(urm, U, S, Vt, uTest, K, True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recommendation for user with user id 48515\n",
      "The number 1 recommended song is Creil City BY Alliance Ethnik\n",
      "The number 2 recommended song is Le Jardin d'Hiver BY Jacky Terrasson\n",
      "The number 3 recommended song is What We Do (Explicit) (Feat. Memphis Bleek) BY Sauce Money Featuring Memphis Bleek\n",
      "The number 4 recommended song is Cudi Zone BY Kid Cudi\n",
      "The number 5 recommended song is Kid A BY Radiohead\n",
      "The number 6 recommended song is Good Things BY Rich Boy / Polow Da Don / Keri Hilson\n",
      "The number 7 recommended song is Up Up & Away BY Kid Cudi\n",
      "The number 8 recommended song is Home BY Edward Sharpe & The Magnetic Zeros\n",
      "The number 9 recommended song is Sit Down. Stand Up BY Radiohead\n",
      "The number 10 recommended song is Epiphany BY Johnny Depp_ Helena Bonham Carter_ Jamie Campbell Bower\n",
      "The number 11 recommended song is Pursuit Of Happiness (nightmare) BY Kid Cudi / MGMT / Ratatat\n",
      "The number 12 recommended song is None Shall Pass (Main) BY Aesop Rock\n",
      "The number 13 recommended song is Le Freak BY Chic\n",
      "The number 14 recommended song is One Beat BY Sleater-kinney\n",
      "The number 15 recommended song is Setting Forth BY Eddie Vedder\n",
      "The number 16 recommended song is You're Gonna Miss Me When I'm Gone BY Brooks & Dunn\n",
      "The number 17 recommended song is The Funeral (Album Version) BY Band Of Horses\n",
      "The number 18 recommended song is One Touch BY LCD Soundsystem\n",
      "The number 19 recommended song is U Should Have Killed Me When U Had The Chance BY A Day To Remember\n",
      "The number 20 recommended song is 23 (Album Version) BY Jimmy Eat World\n"
     ]
    }
   ],
   "source": [
    "for user in uTest:\n",
    "    song_dict=[]\n",
    "    print(\"Recommendation for user with user id {}\". format(user))\n",
    "    rank_value = 1\n",
    "    for i in uTest_recommended_items[user,0:20]:\n",
    "        song_details = matrix_decomposition[matrix_decomposition.so_index_value == i].drop_duplicates('so_index_value')[['title','artist_name']]\n",
    "        print(\"The number {} recommended song is {} BY {}\".format(rank_value, list(song_details['title'])[0],list(song_details['artist_name'])[0]))\n",
    "        song_dict.append(list(song_details['title'])[0])\n",
    "        rank_value+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Creil City',\n",
       " \"Le Jardin d'Hiver\",\n",
       " 'What We Do (Explicit) (Feat. Memphis Bleek)',\n",
       " 'Cudi Zone',\n",
       " 'Kid A',\n",
       " 'Good Things',\n",
       " 'Up Up & Away',\n",
       " 'Home',\n",
       " 'Sit Down. Stand Up',\n",
       " 'Epiphany',\n",
       " 'Pursuit Of Happiness (nightmare)',\n",
       " 'None Shall Pass (Main)',\n",
       " 'Le Freak',\n",
       " 'One Beat',\n",
       " 'Setting Forth',\n",
       " \"You're Gonna Miss Me When I'm Gone\",\n",
       " 'The Funeral (Album Version)',\n",
       " 'One Touch',\n",
       " 'U Should Have Killed Me When U Had The Chance',\n",
       " '23 (Album Version)']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "song_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user</th>\n",
       "      <th>song</th>\n",
       "      <th>listen_count</th>\n",
       "      <th>title</th>\n",
       "      <th>release</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>year</th>\n",
       "      <th>total_listen_count</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5232558</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOZFCVU12A8C1338C8</td>\n",
       "      <td>33</td>\n",
       "      <td>L'art et la maniÃ¨re</td>\n",
       "      <td>Mon Paradis</td>\n",
       "      <td>Christophe MaÃ©</td>\n",
       "      <td>2007</td>\n",
       "      <td>427</td>\n",
       "      <td>0.077283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232524</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODWGLU12A8C1386F4</td>\n",
       "      <td>33</td>\n",
       "      <td>Slave To The Blues</td>\n",
       "      <td>Ma Rainey Vol. 3 (1925-1926)</td>\n",
       "      <td>Ma Rainey</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.077283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232546</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMMIUM12A8C13FD49</td>\n",
       "      <td>28</td>\n",
       "      <td>World News</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.065574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232537</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOIDEJQ12AB01888CD</td>\n",
       "      <td>26</td>\n",
       "      <td>The Revolution (LP Version)</td>\n",
       "      <td>Tomb Raider - Music From The Motion Picture To...</td>\n",
       "      <td>BT</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.060890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232535</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOHOSMK12AB01809D9</td>\n",
       "      <td>25</td>\n",
       "      <td>Stranger Things</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.058548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232525</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODXFAS12A58A7B24B</td>\n",
       "      <td>21</td>\n",
       "      <td>Sympathy For The Devil</td>\n",
       "      <td>Greatest Hits</td>\n",
       "      <td>Guns N' Roses</td>\n",
       "      <td>1994</td>\n",
       "      <td>427</td>\n",
       "      <td>0.049180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232541</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOKRZAH12A6D4F7775</td>\n",
       "      <td>20</td>\n",
       "      <td>Closer To The Heart</td>\n",
       "      <td>Retrospective I (1974-1980)</td>\n",
       "      <td>Rush</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.046838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232539</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOJGMYY12AB01809BE</td>\n",
       "      <td>18</td>\n",
       "      <td>Who Knows Who Cares</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.042155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232536</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOIBMQT12AB0181395</td>\n",
       "      <td>18</td>\n",
       "      <td>Clobberin' Time</td>\n",
       "      <td>Live In A Dive</td>\n",
       "      <td>Sick Of It All</td>\n",
       "      <td>2002</td>\n",
       "      <td>427</td>\n",
       "      <td>0.042155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232542</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOLOVPR12AB0182D03</td>\n",
       "      <td>14</td>\n",
       "      <td>Shape Shifter</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.032787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232520</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOCHBAJ12AAF3B3A4F</td>\n",
       "      <td>14</td>\n",
       "      <td>Camera Talk</td>\n",
       "      <td>Camera Talk</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.032787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232521</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOCNAXF12A6D4F9B34</td>\n",
       "      <td>14</td>\n",
       "      <td>Creil City</td>\n",
       "      <td>Fat Come Back</td>\n",
       "      <td>Alliance Ethnik</td>\n",
       "      <td>1999</td>\n",
       "      <td>427</td>\n",
       "      <td>0.032787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232518</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOASTSF12A8C1420B5</td>\n",
       "      <td>14</td>\n",
       "      <td>Welk</td>\n",
       "      <td>Dolores</td>\n",
       "      <td>Bohren &amp; Der Club Of Gore</td>\n",
       "      <td>2008</td>\n",
       "      <td>427</td>\n",
       "      <td>0.032787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232534</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOGLRAT12A6D4FC502</td>\n",
       "      <td>13</td>\n",
       "      <td>Introducing Tobacco To Civilization (LP Version)</td>\n",
       "      <td>Something Like This...The Bob Newhart Anthology</td>\n",
       "      <td>Bob Newhart</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232533</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOGBGBT12AB01809B3</td>\n",
       "      <td>13</td>\n",
       "      <td>Warning Sign</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232559</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOZFJYG12AB0182D24</td>\n",
       "      <td>13</td>\n",
       "      <td>Cards &amp; Quarters</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232543</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOLVTNS12AB01809E2</td>\n",
       "      <td>13</td>\n",
       "      <td>Sticky Thread</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232548</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SONIDAJ12AB01809D1</td>\n",
       "      <td>13</td>\n",
       "      <td>Cubism Dream</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232557</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOYTDBL12A6D4F8148</td>\n",
       "      <td>13</td>\n",
       "      <td>What We Do (Explicit) (Feat. Memphis Bleek)</td>\n",
       "      <td>Middle Finger U</td>\n",
       "      <td>Sauce Money Featuring Memphis Bleek</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.030445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232519</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOBDRND12A8C13FD08</td>\n",
       "      <td>12</td>\n",
       "      <td>Sun Hands</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.028103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232526</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODXNBO12A8C14414E</td>\n",
       "      <td>9</td>\n",
       "      <td>Wide Eyes</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.021077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232523</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODRFRJ12A8C144167</td>\n",
       "      <td>8</td>\n",
       "      <td>Airplanes</td>\n",
       "      <td>Gorilla Manor</td>\n",
       "      <td>Local Natives</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.018735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232531</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOFEIGH12AF72A0485</td>\n",
       "      <td>7</td>\n",
       "      <td>Unity (Radio Edit)</td>\n",
       "      <td>Unity (Radio Edit)</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.016393</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232554</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOVAJNX12A6D4F6E2F</td>\n",
       "      <td>5</td>\n",
       "      <td>Other Ways</td>\n",
       "      <td>Shrek The Third</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>2007</td>\n",
       "      <td>427</td>\n",
       "      <td>0.011710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232545</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMCMKG12A8C1347BF</td>\n",
       "      <td>5</td>\n",
       "      <td>Le Jardin d'Hiver</td>\n",
       "      <td>Smile</td>\n",
       "      <td>Jacky Terrasson</td>\n",
       "      <td>2002</td>\n",
       "      <td>427</td>\n",
       "      <td>0.011710</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232547</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SONGVLR12A58A789CC</td>\n",
       "      <td>3</td>\n",
       "      <td>Where's The Love</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.007026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232538</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOIUUTM12AB01868F2</td>\n",
       "      <td>3</td>\n",
       "      <td>The Lime Tree</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>2005</td>\n",
       "      <td>427</td>\n",
       "      <td>0.007026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232528</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOELGRP12A3F1EA18F</td>\n",
       "      <td>3</td>\n",
       "      <td>Origami Crane</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>Trevor Hall</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.007026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232550</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SORMOAU12AB018956B</td>\n",
       "      <td>2</td>\n",
       "      <td>Sky Might Fall</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.004684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232544</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOMBNVR12AF72A736E</td>\n",
       "      <td>1</td>\n",
       "      <td>Why Georgia</td>\n",
       "      <td>Where The Light Is: John Mayer Live In Los Ang...</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232540</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOKJBXF12A58A7CD00</td>\n",
       "      <td>1</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Ink My Whole Body</td>\n",
       "      <td>Wiz Khalifa</td>\n",
       "      <td>2010</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232532</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOFKVPQ12AB018F567</td>\n",
       "      <td>1</td>\n",
       "      <td>Die Tonight</td>\n",
       "      <td>Turn It Around</td>\n",
       "      <td>Comeback Kid</td>\n",
       "      <td>2003</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232530</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOESGET12AB0180504</td>\n",
       "      <td>1</td>\n",
       "      <td>Danse Pour Moi</td>\n",
       "      <td>Danse Pour Moi</td>\n",
       "      <td>M. Pokora</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232529</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOEPWYH12AF72A4813</td>\n",
       "      <td>1</td>\n",
       "      <td>Cudi Zone</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232549</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOPIEIJ12A6D4F8F81</td>\n",
       "      <td>1</td>\n",
       "      <td>Waterfall</td>\n",
       "      <td>Ã La Carte</td>\n",
       "      <td>Triumvirat</td>\n",
       "      <td>1978</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232551</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOSFAQM12AB018427B</td>\n",
       "      <td>1</td>\n",
       "      <td>Solo Dolo (nightmare)</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232552</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOTHABI12A58A7DACB</td>\n",
       "      <td>1</td>\n",
       "      <td>Perfectly Lonely</td>\n",
       "      <td>Battle Studies</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232553</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOUQZHQ12A8C143FD3</td>\n",
       "      <td>1</td>\n",
       "      <td>As I Em</td>\n",
       "      <td>Asleep In The Bread Aisle</td>\n",
       "      <td>Asher Roth / Chester French</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232555</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOWQMMX12AB017DBE2</td>\n",
       "      <td>1</td>\n",
       "      <td>Day 'N' Nite</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2008</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232556</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOXNZOW12AB017F756</td>\n",
       "      <td>1</td>\n",
       "      <td>Half Of My Heart</td>\n",
       "      <td>Battle Studies</td>\n",
       "      <td>John Mayer</td>\n",
       "      <td>0</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232527</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOEFBYT12AB01842B2</td>\n",
       "      <td>1</td>\n",
       "      <td>My World</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi / Billy Craven</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232522</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SODHZLF12A8C136CC1</td>\n",
       "      <td>1</td>\n",
       "      <td>Wake The Dead (Album Version)</td>\n",
       "      <td>Wake The Dead</td>\n",
       "      <td>Comeback Kid</td>\n",
       "      <td>2005</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232517</th>\n",
       "      <td>050834fcdba55aa3e6ada1d4f2949002ee817254</td>\n",
       "      <td>SOARUPP12AB01842E0</td>\n",
       "      <td>1</td>\n",
       "      <td>Up Up &amp; Away</td>\n",
       "      <td>Man On The Moon: The End Of Day</td>\n",
       "      <td>Kid Cudi</td>\n",
       "      <td>2009</td>\n",
       "      <td>427</td>\n",
       "      <td>0.002342</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             user                song  \\\n",
       "5232558  050834fcdba55aa3e6ada1d4f2949002ee817254  SOZFCVU12A8C1338C8   \n",
       "5232524  050834fcdba55aa3e6ada1d4f2949002ee817254  SODWGLU12A8C1386F4   \n",
       "5232546  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMMIUM12A8C13FD49   \n",
       "5232537  050834fcdba55aa3e6ada1d4f2949002ee817254  SOIDEJQ12AB01888CD   \n",
       "5232535  050834fcdba55aa3e6ada1d4f2949002ee817254  SOHOSMK12AB01809D9   \n",
       "5232525  050834fcdba55aa3e6ada1d4f2949002ee817254  SODXFAS12A58A7B24B   \n",
       "5232541  050834fcdba55aa3e6ada1d4f2949002ee817254  SOKRZAH12A6D4F7775   \n",
       "5232539  050834fcdba55aa3e6ada1d4f2949002ee817254  SOJGMYY12AB01809BE   \n",
       "5232536  050834fcdba55aa3e6ada1d4f2949002ee817254  SOIBMQT12AB0181395   \n",
       "5232542  050834fcdba55aa3e6ada1d4f2949002ee817254  SOLOVPR12AB0182D03   \n",
       "5232520  050834fcdba55aa3e6ada1d4f2949002ee817254  SOCHBAJ12AAF3B3A4F   \n",
       "5232521  050834fcdba55aa3e6ada1d4f2949002ee817254  SOCNAXF12A6D4F9B34   \n",
       "5232518  050834fcdba55aa3e6ada1d4f2949002ee817254  SOASTSF12A8C1420B5   \n",
       "5232534  050834fcdba55aa3e6ada1d4f2949002ee817254  SOGLRAT12A6D4FC502   \n",
       "5232533  050834fcdba55aa3e6ada1d4f2949002ee817254  SOGBGBT12AB01809B3   \n",
       "5232559  050834fcdba55aa3e6ada1d4f2949002ee817254  SOZFJYG12AB0182D24   \n",
       "5232543  050834fcdba55aa3e6ada1d4f2949002ee817254  SOLVTNS12AB01809E2   \n",
       "5232548  050834fcdba55aa3e6ada1d4f2949002ee817254  SONIDAJ12AB01809D1   \n",
       "5232557  050834fcdba55aa3e6ada1d4f2949002ee817254  SOYTDBL12A6D4F8148   \n",
       "5232519  050834fcdba55aa3e6ada1d4f2949002ee817254  SOBDRND12A8C13FD08   \n",
       "5232526  050834fcdba55aa3e6ada1d4f2949002ee817254  SODXNBO12A8C14414E   \n",
       "5232523  050834fcdba55aa3e6ada1d4f2949002ee817254  SODRFRJ12A8C144167   \n",
       "5232531  050834fcdba55aa3e6ada1d4f2949002ee817254  SOFEIGH12AF72A0485   \n",
       "5232554  050834fcdba55aa3e6ada1d4f2949002ee817254  SOVAJNX12A6D4F6E2F   \n",
       "5232545  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMCMKG12A8C1347BF   \n",
       "5232547  050834fcdba55aa3e6ada1d4f2949002ee817254  SONGVLR12A58A789CC   \n",
       "5232538  050834fcdba55aa3e6ada1d4f2949002ee817254  SOIUUTM12AB01868F2   \n",
       "5232528  050834fcdba55aa3e6ada1d4f2949002ee817254  SOELGRP12A3F1EA18F   \n",
       "5232550  050834fcdba55aa3e6ada1d4f2949002ee817254  SORMOAU12AB018956B   \n",
       "5232544  050834fcdba55aa3e6ada1d4f2949002ee817254  SOMBNVR12AF72A736E   \n",
       "5232540  050834fcdba55aa3e6ada1d4f2949002ee817254  SOKJBXF12A58A7CD00   \n",
       "5232532  050834fcdba55aa3e6ada1d4f2949002ee817254  SOFKVPQ12AB018F567   \n",
       "5232530  050834fcdba55aa3e6ada1d4f2949002ee817254  SOESGET12AB0180504   \n",
       "5232529  050834fcdba55aa3e6ada1d4f2949002ee817254  SOEPWYH12AF72A4813   \n",
       "5232549  050834fcdba55aa3e6ada1d4f2949002ee817254  SOPIEIJ12A6D4F8F81   \n",
       "5232551  050834fcdba55aa3e6ada1d4f2949002ee817254  SOSFAQM12AB018427B   \n",
       "5232552  050834fcdba55aa3e6ada1d4f2949002ee817254  SOTHABI12A58A7DACB   \n",
       "5232553  050834fcdba55aa3e6ada1d4f2949002ee817254  SOUQZHQ12A8C143FD3   \n",
       "5232555  050834fcdba55aa3e6ada1d4f2949002ee817254  SOWQMMX12AB017DBE2   \n",
       "5232556  050834fcdba55aa3e6ada1d4f2949002ee817254  SOXNZOW12AB017F756   \n",
       "5232527  050834fcdba55aa3e6ada1d4f2949002ee817254  SOEFBYT12AB01842B2   \n",
       "5232522  050834fcdba55aa3e6ada1d4f2949002ee817254  SODHZLF12A8C136CC1   \n",
       "5232517  050834fcdba55aa3e6ada1d4f2949002ee817254  SOARUPP12AB01842E0   \n",
       "\n",
       "         listen_count                                             title  \\\n",
       "5232558            33                              L'art et la maniÃ¨re   \n",
       "5232524            33                                Slave To The Blues   \n",
       "5232546            28                                        World News   \n",
       "5232537            26                       The Revolution (LP Version)   \n",
       "5232535            25                                   Stranger Things   \n",
       "5232525            21                            Sympathy For The Devil   \n",
       "5232541            20                               Closer To The Heart   \n",
       "5232539            18                               Who Knows Who Cares   \n",
       "5232536            18                                   Clobberin' Time   \n",
       "5232542            14                                     Shape Shifter   \n",
       "5232520            14                                       Camera Talk   \n",
       "5232521            14                                        Creil City   \n",
       "5232518            14                                              Welk   \n",
       "5232534            13  Introducing Tobacco To Civilization (LP Version)   \n",
       "5232533            13                                      Warning Sign   \n",
       "5232559            13                                  Cards & Quarters   \n",
       "5232543            13                                     Sticky Thread   \n",
       "5232548            13                                      Cubism Dream   \n",
       "5232557            13       What We Do (Explicit) (Feat. Memphis Bleek)   \n",
       "5232519            12                                         Sun Hands   \n",
       "5232526             9                                         Wide Eyes   \n",
       "5232523             8                                         Airplanes   \n",
       "5232531             7                                Unity (Radio Edit)   \n",
       "5232554             5                                        Other Ways   \n",
       "5232545             5                                 Le Jardin d'Hiver   \n",
       "5232547             3                                  Where's The Love   \n",
       "5232538             3                                     The Lime Tree   \n",
       "5232528             3                                     Origami Crane   \n",
       "5232550             2                                    Sky Might Fall   \n",
       "5232544             1                                       Why Georgia   \n",
       "5232540             1                                 Ink My Whole Body   \n",
       "5232532             1                                       Die Tonight   \n",
       "5232530             1                                    Danse Pour Moi   \n",
       "5232529             1                                         Cudi Zone   \n",
       "5232549             1                                         Waterfall   \n",
       "5232551             1                             Solo Dolo (nightmare)   \n",
       "5232552             1                                  Perfectly Lonely   \n",
       "5232553             1                                           As I Em   \n",
       "5232555             1                                      Day 'N' Nite   \n",
       "5232556             1                                  Half Of My Heart   \n",
       "5232527             1                                          My World   \n",
       "5232522             1                     Wake The Dead (Album Version)   \n",
       "5232517             1                                      Up Up & Away   \n",
       "\n",
       "                                                   release  \\\n",
       "5232558                                        Mon Paradis   \n",
       "5232524                       Ma Rainey Vol. 3 (1925-1926)   \n",
       "5232546                                      Gorilla Manor   \n",
       "5232537  Tomb Raider - Music From The Motion Picture To...   \n",
       "5232535                                      Gorilla Manor   \n",
       "5232525                                      Greatest Hits   \n",
       "5232541                        Retrospective I (1974-1980)   \n",
       "5232539                                      Gorilla Manor   \n",
       "5232536                                     Live In A Dive   \n",
       "5232542                                      Gorilla Manor   \n",
       "5232520                                        Camera Talk   \n",
       "5232521                                      Fat Come Back   \n",
       "5232518                                            Dolores   \n",
       "5232534    Something Like This...The Bob Newhart Anthology   \n",
       "5232533                                      Gorilla Manor   \n",
       "5232559                                      Gorilla Manor   \n",
       "5232543                                      Gorilla Manor   \n",
       "5232548                                      Gorilla Manor   \n",
       "5232557                                    Middle Finger U   \n",
       "5232519                                      Gorilla Manor   \n",
       "5232526                                      Gorilla Manor   \n",
       "5232523                                      Gorilla Manor   \n",
       "5232531                                 Unity (Radio Edit)   \n",
       "5232554                                    Shrek The Third   \n",
       "5232545                                              Smile   \n",
       "5232547                                        Trevor Hall   \n",
       "5232538                                        Trevor Hall   \n",
       "5232528                                        Trevor Hall   \n",
       "5232550                    Man On The Moon: The End Of Day   \n",
       "5232544  Where The Light Is: John Mayer Live In Los Ang...   \n",
       "5232540                                  Ink My Whole Body   \n",
       "5232532                                     Turn It Around   \n",
       "5232530                                     Danse Pour Moi   \n",
       "5232529                    Man On The Moon: The End Of Day   \n",
       "5232549                                        Ã La Carte   \n",
       "5232551                    Man On The Moon: The End Of Day   \n",
       "5232552                                     Battle Studies   \n",
       "5232553                          Asleep In The Bread Aisle   \n",
       "5232555                    Man On The Moon: The End Of Day   \n",
       "5232556                                     Battle Studies   \n",
       "5232527                    Man On The Moon: The End Of Day   \n",
       "5232522                                      Wake The Dead   \n",
       "5232517                    Man On The Moon: The End Of Day   \n",
       "\n",
       "                                 artist_name  year  total_listen_count  \\\n",
       "5232558                      Christophe MaÃ©  2007                 427   \n",
       "5232524                            Ma Rainey     0                 427   \n",
       "5232546                        Local Natives  2009                 427   \n",
       "5232537                                   BT     0                 427   \n",
       "5232535                        Local Natives  2009                 427   \n",
       "5232525                        Guns N' Roses  1994                 427   \n",
       "5232541                                 Rush     0                 427   \n",
       "5232539                        Local Natives  2009                 427   \n",
       "5232536                       Sick Of It All  2002                 427   \n",
       "5232542                        Local Natives  2009                 427   \n",
       "5232520                        Local Natives  2009                 427   \n",
       "5232521                      Alliance Ethnik  1999                 427   \n",
       "5232518            Bohren & Der Club Of Gore  2008                 427   \n",
       "5232534                          Bob Newhart     0                 427   \n",
       "5232533                        Local Natives  2009                 427   \n",
       "5232559                        Local Natives  2009                 427   \n",
       "5232543                        Local Natives  2009                 427   \n",
       "5232548                        Local Natives  2009                 427   \n",
       "5232557  Sauce Money Featuring Memphis Bleek     0                 427   \n",
       "5232519                        Local Natives  2009                 427   \n",
       "5232526                        Local Natives  2009                 427   \n",
       "5232523                        Local Natives  2009                 427   \n",
       "5232531                          Trevor Hall  2009                 427   \n",
       "5232554                          Trevor Hall  2007                 427   \n",
       "5232545                      Jacky Terrasson  2002                 427   \n",
       "5232547                          Trevor Hall  2009                 427   \n",
       "5232538                          Trevor Hall  2005                 427   \n",
       "5232528                          Trevor Hall  2009                 427   \n",
       "5232550                             Kid Cudi  2009                 427   \n",
       "5232544                           John Mayer     0                 427   \n",
       "5232540                          Wiz Khalifa  2010                 427   \n",
       "5232532                         Comeback Kid  2003                 427   \n",
       "5232530                            M. Pokora     0                 427   \n",
       "5232529                             Kid Cudi  2009                 427   \n",
       "5232549                           Triumvirat  1978                 427   \n",
       "5232551                             Kid Cudi  2009                 427   \n",
       "5232552                           John Mayer     0                 427   \n",
       "5232553          Asher Roth / Chester French  2009                 427   \n",
       "5232555                             Kid Cudi  2008                 427   \n",
       "5232556                           John Mayer     0                 427   \n",
       "5232527              Kid Cudi / Billy Craven  2009                 427   \n",
       "5232522                         Comeback Kid  2005                 427   \n",
       "5232517                             Kid Cudi  2009                 427   \n",
       "\n",
       "            score  \n",
       "5232558  0.077283  \n",
       "5232524  0.077283  \n",
       "5232546  0.065574  \n",
       "5232537  0.060890  \n",
       "5232535  0.058548  \n",
       "5232525  0.049180  \n",
       "5232541  0.046838  \n",
       "5232539  0.042155  \n",
       "5232536  0.042155  \n",
       "5232542  0.032787  \n",
       "5232520  0.032787  \n",
       "5232521  0.032787  \n",
       "5232518  0.032787  \n",
       "5232534  0.030445  \n",
       "5232533  0.030445  \n",
       "5232559  0.030445  \n",
       "5232543  0.030445  \n",
       "5232548  0.030445  \n",
       "5232557  0.030445  \n",
       "5232519  0.028103  \n",
       "5232526  0.021077  \n",
       "5232523  0.018735  \n",
       "5232531  0.016393  \n",
       "5232554  0.011710  \n",
       "5232545  0.011710  \n",
       "5232547  0.007026  \n",
       "5232538  0.007026  \n",
       "5232528  0.007026  \n",
       "5232550  0.004684  \n",
       "5232544  0.002342  \n",
       "5232540  0.002342  \n",
       "5232532  0.002342  \n",
       "5232530  0.002342  \n",
       "5232529  0.002342  \n",
       "5232549  0.002342  \n",
       "5232551  0.002342  \n",
       "5232552  0.002342  \n",
       "5232553  0.002342  \n",
       "5232555  0.002342  \n",
       "5232556  0.002342  \n",
       "5232527  0.002342  \n",
       "5232522  0.002342  \n",
       "5232517  0.002342  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_id = list(triplet_dataset_sub_song_merged.user)[0]\n",
    "test_data_user=triplet_dataset_sub_song_merged[triplet_dataset_sub_song_merged['user']=='050834fcdba55aa3e6ada1d4f2949002ee817254']#找出该用户在测试集上听过的所有歌曲\n",
    "#test_data_user.head(20)\n",
    "test_data_user_sort = test_data_user.sort_values(by = 'listen_count', ascending = False)\n",
    "test_data_user_sort.head(n =43)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(43, 9)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data_user_sort.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Number_of_success=test_data_user_sort.title.isin(song_dict).sum()\n",
    "Number_of_success"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the accuracy is 0.25\n"
     ]
    }
   ],
   "source": [
    "Precision=Number_of_success/20.0\n",
    "print \"the accuracy is %.2f\" %Precision"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the Recall is 0.116\n"
     ]
    }
   ],
   "source": [
    "Recall=Number_of_success/(float)(test_data_user_sort.shape[0])\n",
    "print \"the Recall is %.3f\" %Recall"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里可以看到基于矩阵分解的准确率在25%左右，召回率在11.6%左右。这里选取与前面使用音乐相似度的方式测量的结果有点偏差，基于矩阵分解的方式计算速度比基于物品相似度的协同过滤要快很多，基于物品相似度的协同过滤每个用户要计算30分钟以上，这里只需要几分钟就可以。因此优先选择基于矩阵的方式，速度会很快。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nuTest = [7]\\nuTest_recommended_items = compute_estimated_matrix(urm, U, S, Vt, uTest, K, True)\\nfor user in uTest:\\n    song_dict=[]\\n    print(\"Recommendation for user with user id {}\". format(user))\\n    rank_value = 1\\n    for i in uTest_recommended_items[user,0:20]:\\n        song_details = matrix_decomposition[matrix_decomposition.so_index_value == i].drop_duplicates(\\'so_index_value\\')[[\\'title\\',\\'artist_name\\']]\\n        print(\"The number {} recommended song is {} BY {}\".format(rank_value, list(song_details[\\'title\\'])[0],list(song_details[\\'artist_name\\'])[0]))\\n        song_dict.append(list(song_details[\\'title\\'])[0])\\n        rank_value+=1\\nuser_id = list(triplet_dataset_sub_song_merged.user)[0]\\ntest_data_user=triplet_dataset_sub_song_merged[triplet_dataset_sub_song_merged[\\'user\\']==\\'b61afb42335287239bd40e1dea50d849cbf8a9a9\\']#\\xe6\\x89\\xbe\\xe5\\x87\\xba\\xe8\\xaf\\xa5\\xe7\\x94\\xa8\\xe6\\x88\\xb7\\xe5\\x9c\\xa8\\xe6\\xb5\\x8b\\xe8\\xaf\\x95\\xe9\\x9b\\x86\\xe4\\xb8\\x8a\\xe5\\x90\\xac\\xe8\\xbf\\x87\\xe7\\x9a\\x84\\xe6\\x89\\x80\\xe6\\x9c\\x89\\xe6\\xad\\x8c\\xe6\\x9b\\xb2\\ntest_data_user.head(20)\\ntest_data_user_sort = test_data_user.sort_values(by = \\'listen_count\\', ascending = False)\\ntest_data_user_sort.head(n =43)\\nNumber_of_success=test_data_user_sort.title.isin(song_dict).sum()\\nPrecision=Number_of_success/20.0\\nprint \"the accuracy is %.2f\" %Precision\\nRecall=Number_of_success/(float)(test_data_user_sort.shape[0])\\nprint \"the Recall is %.3f\" %Recall\\n'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "uTest = [7]\n",
    "uTest_recommended_items = compute_estimated_matrix(urm, U, S, Vt, uTest, K, True)\n",
    "for user in uTest:\n",
    "    song_dict=[]\n",
    "    print(\"Recommendation for user with user id {}\". format(user))\n",
    "    rank_value = 1\n",
    "    for i in uTest_recommended_items[user,0:20]:\n",
    "        song_details = matrix_decomposition[matrix_decomposition.so_index_value == i].drop_duplicates('so_index_value')[['title','artist_name']]\n",
    "        print(\"The number {} recommended song is {} BY {}\".format(rank_value, list(song_details['title'])[0],list(song_details['artist_name'])[0]))\n",
    "        song_dict.append(list(song_details['title'])[0])\n",
    "        rank_value+=1\n",
    "user_id = list(triplet_dataset_sub_song_merged.user)[0]\n",
    "test_data_user=triplet_dataset_sub_song_merged[triplet_dataset_sub_song_merged['user']=='b61afb42335287239bd40e1dea50d849cbf8a9a9']#找出该用户在测试集上听过的所有歌曲\n",
    "test_data_user.head(20)\n",
    "test_data_user_sort = test_data_user.sort_values(by = 'listen_count', ascending = False)\n",
    "test_data_user_sort.head(n =43)\n",
    "Number_of_success=test_data_user_sort.title.isin(song_dict).sum()\n",
    "Precision=Number_of_success/20.0\n",
    "print \"the accuracy is %.2f\" %Precision\n",
    "Recall=Number_of_success/(float)(test_data_user_sort.shape[0])\n",
    "print \"the Recall is %.3f\" %Recall\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  },
  "toc": {
   "nav_menu": {
    "height": "153px",
    "width": "160px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": true,
   "toc_position": {
    "height": "691px",
    "left": "0px",
    "right": "1405px",
    "top": "106px",
    "width": "303px"
   },
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
